package com.chwn.curiousdiary.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.ui.login.LoginActivity;
import com.chwn.harulib.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Created by Myshikin on 2017/6/18.
 */

public class BabySexActivity extends BaseActivity {


    @BindView(R.id.top_view)
    View      topView;
    @BindView(R.id.tv_toolbar_title)
    TextView  tvToolbarTitle;
    @BindView(R.id.iv_sex_boy)
    ImageView mBoyIV;
    @BindView(R.id.tv_sex_boy)
    TextView  mBoyTV;
    @BindView(R.id.iv_sex_girl)
    ImageView mGirlIV;
    @BindView(R.id.tv_sex_girl)
    TextView  mGirlTV;

    /**
     * 1--男生 0--女生
     */
    int sex = -1;
    private String phone;
    private String unionid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_baby_sex;
    }

    public void onEventMainThread(CustomEvent event) {
        switch (event.getState()) {
            case CustomEvent.ClOSE_SELF:
                finish();
                if (EventBus.getDefault().isRegistered(this)) {
                    EventBus.getDefault().unregister(this);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        phone = getIntent().getStringExtra(Constant.PHONE);
        unionid = getIntent().getStringExtra(Constant.UNION_ID);
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setBackTVisible(true);
        setBackIcon(R.drawable.icon_back_light);
    }

    @OnClick({R.id.ll_back, R.id.ll_sex_boy, R.id.ll_sex_girl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_sex_boy:
                sex = 1;
                setSexChioce();
                break;
            case R.id.ll_sex_girl:
                sex = 0;
                setSexChioce();
                break;
        }
    }

    private void setSexChioce() {
        mBoyIV.setSelected(sex == 1);
        mBoyTV.setSelected(sex == 1);
        mGirlIV.setSelected(sex == 0);
        mGirlTV.setSelected(sex == 0);

        Intent intent = new Intent(this, BirthDayActivty.class);
        intent.putExtra(Constant.PHONE, phone);
        intent.putExtra(Constant.UNION_ID, unionid);
        intent.putExtra(Constant.BABYINFO, sex);
        startActivity(intent);
    }

    /* @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {

         if (keyCode == KeyEvent.KEYCODE_BACK) {
             back();
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }
 */
    private void back() {
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }
}
